var network = require("../../utils/NetWorkUtils.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    region: ['上海市','上海市',''],
    customItem: '全部',
    focus: false,
    name:'',//姓名
    mobile:'',//手机
    address:'',//详细地址
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      focus: true,
      addressFocus: false
    })

    this.saveAddress('','','','','','')
    // this.inputWacth();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  // 选择城市
  bindRegionChange: function(e) {
    console.log(e)
    this.setData({
      region: e.detail.value
    })
  },
  formSubmit: function(e) {
    console.log(this.data.region[0])
    console.log(this.data.region[2])
    console.log(e.detail.value)
    if (!e.detail.value.name) {
      wx.showToast({
        title: '请输入收件人姓名',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (!e.detail.value.mobile){
      console.log(e.detail.value)
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none',
        duration: 2000
      })
      return;
    }

    if (e.detail.value.mobile.length<11) {
      console.log(e.detail.value)
      wx.showToast({
        title: '请输入正确手机号码',
        icon: 'none',
        duration: 2000
      })
      return;
    }

    if (!this.data.region[0] || this.data.region[0] == "全部" || this.data.region[1] == "全部"){
      wx.showToast({
        title: '请选择城市',
        icon: 'none',
        duration: 2000
      })
      return;
    }


    if (!e.detail.value.address) {
      wx.showToast({
        title: '请输入详细地址',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    this.saveAddress(e.detail.value.name, e.detail.value.mobile, this.data.region[0], this.data.region[1], this.data.region[2], e.detail.value.address)
  },

  saveAddress: function (name, mobile, province, city, district, address){
    var that=this;
    var dataParam={
      name:name,
      mobile: mobile,
      province: province,
      city: city,
      address: address
    }
    if(!(district == "全部")){
      dataParam["district"] = district;
    }

    network.requestHttp("m=api&c=Index&a=address", dataParam,function(res){
      console.log(res)  

      if(res.data.msg=='保存成功'){
        wx.showToast({
          title: '保存成功',
          icon: 'none',
          duration: 2000
        })
      } else if (res.data.msg == '网络错误，请稍后再试'){
        wx.showToast({
          title: '保存成功',
          icon: 'none',
          duration: 2000
        })
      }
      
    if(res.data.data){
      //先将选择城市数组清空
      that.setData({
        region:[]
      })
      //再将之前保存的数据设置上去
      var addArray=that.data.region
      addArray.push(res.data.data.province)
      addArray.push(res.data.data.city)
      addArray.push(res.data.data.district)
      that.setData({
        name: res.data.data.name,
        mobile: res.data.data.mobile,
        address: res.data.data.address,
        region: addArray
      })
    }

    })
  }

  
})